package com.edu.gdkm.servlet;

import com.edu.gdkm.dao.dypDao;
import com.edu.gdkm.db.stor.*;
import com.edu.gdkm.db.table.*;
import com.edu.gdkm.util.cookiesUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/adminServlet")
public class adminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<user> userList;
        List<admin> adminList;
        sel_card selCard;
        List<sel_orders> orderList = new ArrayList<>();
        List<ticket> ticketList = new ArrayList<>();
        List<ticketNum> ticketNumList = new ArrayList<>();
        List<filmList> filmLists = new ArrayList<>();
        List<videos> videosList;

        try {
            // 打开数据库
            dypDao dao = new dypDao();

            /* 用户列表查询 */
            userList = dao.getUserList(
                    dao.select("*", "customer"));

            /* 管理员列表查询 */
            adminList = dao.getAdminList(
                    dao.select("*", "category"));

            /* 电影分类列表查询 */
            videosList = dao.getVideosList(
                    dao.select("*", "videos"));

            /* 卡片收益（今日、累计、注册人数） */
            selCard = dao.getSelCard(
                    dao.selectStor("sel_card"));

            /* 订单列表查询 */
            // 订单号 -> 查询 => 订单表
            for (String o :
                    dao.getAllID(
                            dao.selectOrderBy("oid", "orders"))) {
                orderList.add(  // 新增列表
                        dao.getOrder(   // 封装类
                                dao.selectStors("sel_orders", o)));  // 调用存储过程
            }

            /* 影票列表查询 */
            for (String k : // 影票表（票号） -> 查询 => 影票列表
                    dao.getAllID(
                            dao.selectOrderBy("kid", "filmegener"))) {
                ticketList.add( // 新增列表
                        dao.getTicket(  // 封装类
                                dao.selectStors("sel_filmegener", k)));  // 调用存储过程
            }

            /* 影票售出列表查询 */
            for (String f : // 影票表（电影号）-> 查询 => 影票表（售卖数量）
                    dao.getAllID(
                            dao.selectGroupBy("filmid", "filmegener"))){
                ticketNumList.add(  // 新增列表
                        dao.getTicketNum(   // 封装类
                                dao.selectStors("sel_ticketNums", f)));  // 调用存储过程
            }

            /* 电影列表查询 */
            for (String fid:
                    dao.getAllID(
                            dao.selectOrderBy("filmid", "film"))) {
                filmLists.add(
                        dao.getFilmList(
                                dao.selectStors("sel_film", fid)));
            }

        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        /* 获取当前管理员名 */
        cookiesUtil util = new cookiesUtil();
        String[] split = util.splitCookies(
                util.findCookie(req.getCookies(), "dyp_user"));

        /* 域存储 */
        HttpSession session = req.getSession();
        session.setAttribute("films", filmLists);
        session.setAttribute("orders", orderList);
        session.setAttribute("tickets", ticketList);
        session.setAttribute("users", userList);
        session.setAttribute("admins", adminList);
        session.setAttribute("ticketNums", ticketNumList);
        session.setAttribute("name", split[0]);
        session.setAttribute("card", selCard);
        session.setAttribute("video", videosList);
        // 转发 => 页面 -> 后台管理系统
        req.getRequestDispatcher("admin.jsp?st=0&ed=7").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
